package org.example.servlet;

import org.example.DAO.BlogCommentDAO;
import org.example.entity.BlogComment;
import org.example.DAO.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@WebServlet("/api/comment/list")
public class CommentListServlet extends HttpServlet {
    private static final Logger logger = LoggerFactory.getLogger(CommentListServlet.class);

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Connection conn = null;

        try {
            String blogId = request.getParameter("blogId");
            if (blogId == null || blogId.isEmpty()) {
                out.println("{\"success\": false, \"message\": \"缺少 blogId 参数\"}");
                return;
            }

            conn = DBUtil.getConnection();
            BlogCommentDAO commentDAO = new BlogCommentDAO(conn);

            // 正确调用 BlogCommentDAO 的 getCommentsByBlogId 方法
            List<BlogComment> comments = commentDAO.getCommentsByBlogId(blogId);

            // 手动构建 JSON 响应（这里只是简单示例，实际可优化或用 JSON 库）
            StringBuilder json = new StringBuilder();
            json.append("{\"success\": true, \"data\": [");

            for (int i = 0; i < comments.size(); i++) {
                BlogComment comment = comments.get(i);
                json.append("{")
                        .append("\"commentId\":\"").append(comment.getCommentId()).append("\"")
                        .append(",\"blogId\":\"").append(comment.getBlogId()).append("\"")
                        .append(",\"userId\":\"").append(comment.getUserId()).append("\"")
                        .append(",\"content\":\"").append(comment.getContent()).append("\"")
                        .append(",\"createdAt\":\"").append(comment.getCreatedAt()).append("\"")
                        .append(",\"parentId\":\"").append(comment.getParentId()).append("\"")
                        .append(",\"username\":\"").append(comment.getUsername()).append("\"")
                        .append(",\"avatar\":\"").append(comment.getAvatar()).append("\"")
                        .append("}");

                if (i < comments.size() - 1) {
                    json.append(",");
                }
            }

            json.append("]}");
            out.println(json.toString());

        } catch (SQLException e) {
            logger.error("获取评论列表时发生数据库异常", e); // 替换 printStackTrace
            out.println("{\"success\": false, \"message\": \"数据库错误\"}");
        } finally {
            DBUtil.close(conn);
        }
    }
}